package com.yanu.t2s.controller;

import com.yanu.t2s.entity.Meat;
import com.yanu.t2s.entity.Tuzai;
import com.yanu.t2s.entity.User;
import com.yanu.t2s.service.MeatService;
import com.yanu.t2s.service.TuzaiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("meat")
public class MeatController {
    @Autowired
    MeatService meatService;
    @Autowired
    TuzaiService tuzaiService;
    //已经废弃
    @GetMapping("get/{id_meat}")
    public Meat findByIdMeat(@PathVariable int id_meat) {
        return meatService.findByIdMeat(id_meat);
    }
    //已经废弃
    @GetMapping("get/all")
    public List<Meat> findAllMeat() {
        return meatService.findAllMeat();
    }

    //下面是有用的

    @Transactional()
    @RequestMapping(value = "insertmeat",method={RequestMethod.POST, RequestMethod.GET},produces = "application/json;charset=UTF-8")
    String insertmeat( Meat meat) {
       //猪肉中放的id_tuzai实际上已经是user表里的id。
        int tuzaiid=meat.getId_tuzai();
        System.out.println("看一下前端传值效果");
        System.out.println(tuzaiid);
        Tuzai tuzai=tuzaiService.findByIdTuzai(tuzaiid);
        double weightsum=tuzai.getPigweightsum();
        System.out.println(meat.getWeight_meat());
        if(weightsum<meat.getWeight_meat()) {
            return "该屠宰户拥有的总的猪肉重量不足以在生成这个重量的猪肉产品";
        }else{
            meatService.insertmeat(meat);
            tuzaiService.reduceweight(tuzaiid,meat.getWeight_meat());
            return "生成猪肉成功";
        }
    }

    //属于某个tuzai户的所有猪肉
    @RequestMapping(value = "findAllmeatofonetuzai",method={RequestMethod.POST, RequestMethod.GET},produces = "application/json;charset=UTF-8")
    List<Meat> findAllmeatofonetuzai(@RequestParam int id_tuzai){
        return meatService.findAllmeatofonetuzai(id_tuzai);
    }
    //属于某个Pifa的所有猪肉
    @RequestMapping(value = "findAllmeatofonepifa",method={RequestMethod.POST, RequestMethod.GET},produces = "application/json;charset=UTF-8")
    List<Meat> findAllmeatofonepifa(@RequestParam int id_pifa){
        return meatService.findAllmeatofonepifa(id_pifa);
    }
    //批发市场对屠宰的猪肉购买
    @RequestMapping(value = "buymeatwithpifa",method={RequestMethod.POST, RequestMethod.GET},produces = "application/json;charset=UTF-8")
    void buymeatwithpifa(@RequestParam int id_pifa,@RequestParam int id_meat){
        meatService.buymeatwithpifa(id_pifa,id_meat);
    }
    //零售市场对批发市场的猪肉选购
    @RequestMapping(value = "buymeatwithlingshou",method={RequestMethod.POST, RequestMethod.GET},produces = "application/json;charset=UTF-8")
    void buymeatwithlingshou(@RequestParam int id_lingshou,@RequestParam int id_meat){
        meatService.buymeatwithlingshou(id_lingshou,id_meat);
    }


}
